import {
  _decorator,
  Component,
  ERigidBody2DType,
  game,
  math,
  Node,
  RigidBody2D,
} from "cc";
const { ccclass, property } = _decorator;

const BALL_SPEED = 6;

@ccclass("ball")
export class ball extends Component {
  start() {
    game.on("launch-ball", this.launch, this);
  }

  update(deltaTime: number) {}

  launch() {
    const body = this.node.getComponent(RigidBody2D);
    body.type = ERigidBody2DType.Dynamic;
    body.linearVelocity = math.v2(BALL_SPEED, BALL_SPEED);
  }

  stop() {
    const body = this.node.getComponent(RigidBody2D);

    this.scheduleOnce(() => {
      body.type = ERigidBody2DType.Static;
    });
  }
}
